/**
 * @file launcher_tab.c
 *
 */

/*********************
 *      INCLUDES
 *********************/
#include "lv_watch.h"

#if USE_LV_WATCH_LAUNCHER2 != 0

#include <stdio.h>

/*********************
 *      DEFINES
 *********************/

/**********************
 *      TYPEDEFS
 **********************/

/**********************
 *  STATIC PROTOTYPES
 **********************/
static void launcher_tab_add_app(lv_obj_t * par, uint8_t tab_type);
static void launcher_tab_del_app(lv_obj_t * tab);
/**********************
 *  STATIC VARIABLES
 **********************/
static uint8_t launcher_tab[WATCH_LAUNCHER_TAB_MAX_COUNT];
/**********************
 *      MACROS
 **********************/

/**********************
 *   GLOBAL FUNCTIONS
 **********************/
void launcher_tab_read(void)
{
    uint8_t i = 0;
    nv_watch_launcher_t launcher_info = {0};
    UI_NV_Read_Req(NV_SECTION_UI_LAUNCHER, 0, sizeof(nv_watch_launcher_t), (uint8_t *)&launcher_info);
    for(i = 0; i < WATCH_LAUNCHER_TAB_MAX_COUNT; i++) {
        launcher_tab[i] = launcher_info.tab[i];
    }
}

void launcher_tab_save(void)
{
    uint8_t i = 0;
    nv_watch_launcher_t launcher_info = {0};
    UI_NV_Read_Req(NV_SECTION_UI_LAUNCHER, 0, sizeof(nv_watch_launcher_t), (uint8_t *)&launcher_info);
    for(i = 0; i < WATCH_LAUNCHER_TAB_MAX_COUNT; i++) {
        launcher_info.tab[i] = launcher_tab[i];
    }
    UI_NV_Write_Req(NV_SECTION_UI_LAUNCHER, 0, sizeof(nv_watch_launcher_t), (uint8_t *)&launcher_info);
}

void launcher_tab_set_type(uint8_t index, uint8_t type)
{
    launcher_tab[index] = type;
}

uint8_t launcher_tab_get_type(uint8_t index)
{
    return launcher_tab[index];
}

uint8_t launcher_tab_get_cnt(void)
{
    uint8_t i = 0;
    uint8_t tab_cnt = 0;
    for(i = 0; i < WATCH_LAUNCHER_TAB_MAX_COUNT; i++) {
        if(launcher_tab[i] != 0) {
            tab_cnt++;
        }
    }
    return tab_cnt;
}

void launcher_tab_create_all(lv_obj_t * tileview)
{
    lv_obj_t * tab;
    uint8_t i = 0;
    uint8_t tab_index = 0;
    lv_obj_t * cont;
    lv_launcher_obj_ext_t * ext = lv_obj_get_ext_attr(lv_watch_get_activity_obj(ACT_ID_LAUNCHER));
    for(i = 0; i < WATCH_LAUNCHER_TAB_MAX_COUNT; i++) {
        if(launcher_tab[i] != 0) {
            printf("%s:add type=%d\n", __FUNCTION__, launcher_tab[i]);
            if(LAUNCHER_HOR_SLIDING_LOOP_MODE_ENABLE)
                tab_index = i + 2;
            else
                tab_index = i + 1;
            tab = launcher_tileview_add_tile(tileview, tab_index);
            lv_watch_set_free_num(tab, launcher_tab[i]);
            ext->tab_obj[i] = tab;
            cont = lv_cont_create(tab, NULL);
            lv_obj_add_style(cont, LV_CONT_PART_MAIN, &lv_style_transp_tight);
            lv_obj_set_size(cont, LV_HOR_RES, LV_VER_RES);
            lv_tileview_add_element(tileview, cont);
            lv_obj_set_click(cont, false);
            launcher_tab_add_app(cont, launcher_tab[i]);
        }
    }
}

void launcher_tab_create_one(lv_obj_t * tileview)
{
    lv_obj_t * tab;
    lv_launcher_obj_ext_t * ext = lv_obj_get_ext_attr(lv_watch_get_activity_obj(ACT_ID_LAUNCHER));
    tab = launcher_tileview_add_tile(tileview, 0);
    ext->tab_obj[WATCH_LAUNCHER_TAB_MAX_COUNT] = tab;
}

void launcher_tab_destroy(void)
{
    lv_launcher_obj_ext_t * ext = lv_obj_get_ext_attr(lv_watch_get_activity_obj(ACT_ID_LAUNCHER));
    for(uint8_t i = 0; i < WATCH_LAUNCHER_TAB_MAX_COUNT + 1; i++) {
        if(ext->tab_obj[i]) {
            launcher_tab_del_app(ext->tab_obj[i]);
            lv_obj_del(ext->tab_obj[i]);
        }
        ext->tab_obj[i] = NULL;
    }
}

uint8_t launcher_tab_get_index(launcher_tab_type_t type)
{
    uint8_t i = 0;
    for(i = 0; i < WATCH_LAUNCHER_TAB_MAX_COUNT; i++) {
        if(launcher_tab[i] == type) {
            break;
        }
    }
    return i;
}

void launcher_tab_update_stop(void)
{
    lv_launcher_obj_ext_t * ext = lv_obj_get_ext_attr(lv_watch_get_activity_obj(ACT_ID_LAUNCHER));
    lv_obj_t * cont = NULL;
    for(uint8_t i = 0; i < WATCH_LAUNCHER_TAB_MAX_COUNT + 1; i++) {
        if(ext->tab_obj[i]) {
            if(lv_obj_count_children(ext->tab_obj[i]) > 0) {
                cont = lv_obj_get_child(ext->tab_obj[i], NULL);
                lv_anim_del(cont, NULL);
            }
        }
    }
}

const char * launcher_tab_get_name(uint8_t type)
{
    uint16_t txtId = 0;
    switch(type) {
        case TAB_APP_QRCODE:
            txtId = WATCH_TEXT_ID_CREATE_QRCODE;
            break;
        case TAB_APP_WEATHER:
            txtId = WATCH_TEXT_ID_WEATHER;
            break;
        case TAB_APP_MUSIC:
            txtId = WATCH_TEXT_ID_AUDIO_PLAYER;
            break;
        case TAB_APP_SPORT:
            txtId = WATCH_TEXT_ID_SPORT;
            break;
        case TAB_APP_HEARTRATE:
            txtId = WATCH_TEXT_ID_HEART_RATE;
            break;
        case TAB_APP_SLEEP:
            txtId = WATCH_TEXT_ID_SLEEP;
            break;
        case TAB_APP_CARD:
            return "卡包";
        case TAB_APP_PRESSURE:
            txtId = WATCH_TEXT_ID_PRESSURE;
            break;
        default:
            break;
    }
    return lv_lang_get_text(txtId);
}


char * launcher_tab_get_icon(uint8_t type)
{
    switch(type) {
        case TAB_APP_QRCODE:
            return ICON_PRIMARY_QRCODES;
        case TAB_APP_WEATHER:
            return ICON_WEATHER;
        case TAB_APP_MUSIC:
            return ICON_MUSIC;
        case TAB_APP_SPORT:
            return ICON_SPORT;
        case TAB_APP_HEARTRATE:
            return ICON_HEART_RATE;
        case TAB_APP_SLEEP:
            return ICON_SLEEP;
        case TAB_APP_CARD:
            return ICON_ALBUM;
        case TAB_APP_PRESSURE:
            return ICON_ELEVATION;
        default:
            return  ICON_SETTING;
    }
}

bool launcher_tab_is_added(uint8_t type)
{
    uint8_t i = 0;
    bool find = false;
    for(i = 0; i < WATCH_LAUNCHER_TAB_MAX_COUNT; i++) {
        if(launcher_tab[i] != 0 && type == launcher_tab[i]) {
            find = true;
            break;
        }
    }
    return find;
}

/**********************
 *  STATIC FUNCTIONS
 **********************/
void tab_create_test(lv_obj_t * par, uint8_t type)
{
    lv_obj_t * label = lv_label_create(par, NULL);
    lv_obj_set_style_local_text_font(label, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, &lv_font_ttf_30);
    if(type == TAB_APP_SLEEP) lv_label_set_text(label, "sleep tab test");
    else if(type == TAB_APP_PRESSURE) lv_label_set_text(label, "pressure tab test");
    else if(type == TAB_APP_CARD) lv_label_set_text(label, "card tab test");
    lv_obj_align(label, NULL, LV_ALIGN_CENTER, 0, 0);
}

static void launcher_tab_add_app(lv_obj_t * par, uint8_t tab_type)
{
    switch(tab_type) {
        case TAB_APP_MUSIC:
            audio_player_create_tab(par);
            break;
        case TAB_APP_HEARTRATE:
            heart_rate_create_tab(par);
            break;
        case TAB_APP_SLEEP:
            sleep_create_tab(par);
            break;
        case TAB_APP_SPORT:
            sport_create_tab(par);
            break;
        case TAB_APP_PRESSURE:
            pressure_create_tab(par);
            break;
        case TAB_APP_WEATHER:
            weather_create(par, 0xFF);
            break;
        case TAB_APP_CARD:
            tab_create_test(par, tab_type);
            break;
        case TAB_APP_QRCODE:
            qrcode_create_tab(par);
            break;
        default:
            break;
    }
}

static void launcher_tab_del_app(lv_obj_t * tab)
{
    uint8_t tab_type = lv_watch_get_free_num(tab);
    printf("%s:type = %d cnt = %d\n", __FUNCTION__, tab_type, lv_obj_count_children(tab));
    if(lv_obj_count_children(tab) > 0) {
        lv_obj_t * cont = lv_obj_get_child(tab, NULL);
        switch(tab_type) {
        case TAB_APP_MUSIC:
            audio_player_destroy_tab(cont);
            break;
        case TAB_APP_HEARTRATE:
            heart_rate_destroy_tab(cont);
            break;
        case TAB_APP_SLEEP:
            sleep_destroy_tab(cont);
            break;
        case TAB_APP_SPORT:
        case TAB_APP_PRESSURE:
        case TAB_APP_WEATHER:
        case TAB_APP_CARD:
        case TAB_APP_QRCODE:
        default:
            break;
        }
    }
}

#endif/* USE_LV_WATCH_LAUNCHER2*/
